package com.xidian.yunwudemo.controller;

import com.xidian.yunwudemo.common.Result;
import com.xidian.yunwudemo.dto.ConfigInfoDto;
import com.xidian.yunwudemo.dto.ServerInfoDto;
import com.xidian.yunwudemo.service.CmdService;
import com.xidian.yunwudemo.service.ServiceControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/controlPage")
public class ServiceControlController {

    @Autowired
    ServiceControlService serviceControlService;

    @Autowired
    CmdService cmdService;

    /**==============开机部分：1、获取docker状态；2、获取运行状态；3、获取配置信息====================**/
    @GetMapping("/status")
    public Result getDockerStatus(@RequestParam("prefixName") String dockerName) throws IOException {
        String res = cmdService.dockerService("inspect",dockerName);
        return res.equals("true") ? Result.OK(true) : Result.OK(false);
    }

    @GetMapping("/getServerInfo")
    public Result getServerInfo(@RequestParam("prefixName") String prefixName){
        return Result.OK(serviceControlService.getPortByName(prefixName));
    }
    @GetMapping("/getConfig")
    public Result getConfig(@RequestParam("prefixName") String prefixName){
        List<ConfigInfoDto> config = serviceControlService.getConfigByName(prefixName);
        return Result.OK(config);
    }
    /**
     *
     * @param prefixName
     * @return Result
     * @Author xiawei
     * @Desc 首先尝试开启docker，如果开启失败，则不去数据库中去获取服务器信息，反之则去获取服务器信息
     */
    @GetMapping("/start")
    public Result startDockerServer(@RequestParam("prefixName") String prefixName) throws IOException {
        // step1: 获取docker启动的结果
        String startRes = cmdService.dockerService("start",prefixName);
        // step2: 判断结果是否正确
        ServerInfoDto serverInfo = null;
        // step3: 如果正确，则去获取服务器数据，反之则不获取直接返回错误
        System.out.println(startRes);
        if(startRes.equals("docker start!")){
            serverInfo =  serviceControlService.getPortByName(prefixName);
        }
        return serverInfo == null ? Result.ERROR() : Result.OK(serverInfo);
    }
    @GetMapping("/stop")
    public Result stopServerOneByOne(@RequestParam("prefixName") String prefixName) throws IOException {
        String stopRes = cmdService.dockerService("stop",prefixName);
        return stopRes.equals("docker closed!") ? Result.OK() : Result.ERROR();
    }
    /*===============================配置保存=========================================*/
    @PatchMapping("/adaptive/save")
    public Result patchAdaptiveConfig(@RequestBody List<ConfigInfoDto> configInfos){
        boolean status = serviceControlService.saveConfig(configInfos,"adaptive");
        return status ? Result.OK("自适应认证服务配置文件保存成功") : Result.ERROR("自适应认证服务配置文件保存失败");
    }

    @PatchMapping("/userAuth/save")
    public Result saveUserConfig(@RequestBody List<ConfigInfoDto> configInfos){
        boolean status = serviceControlService.saveConfig(configInfos,"userAuth");
        return status ? Result.OK("用户认证服务配置文件保存成功") : Result.ERROR("用户认证服务配置文件保存失败");
    }

    @PatchMapping("/bioAuth/save")
    public Result saveBioConfig(@RequestBody List<ConfigInfoDto> configInfos){
        boolean status = serviceControlService.saveConfig(configInfos,"bioAuth");
        return status ? Result.OK("生物认证服务配置文件保存成功") : Result.ERROR("生物认证服务配置文件保存失败");
    }

}
